package br.unipar.blog.web.servlet;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import br.unipar.blog.persistence.entity.Artigo;
import br.unipar.blog.persistence.manager.artigo.ArtigoManager;

@Controller
public class ArtigoServlet {

    @Autowired
    private ArtigoManager artigoManager;

    @RequestMapping(method = RequestMethod.GET,
    value = "/artigo")
    public void buscar(HttpServletRequest req, HttpServletResponse resp) {
        DataOutputStream dos = null;
        try {
            List<Artigo> artigoList = artigoManager.listar();
            dos = new DataOutputStream(resp.getOutputStream());
            dos.writeInt(artigoList.size());
            for (Artigo artigo : artigoList) {
                dos.writeUTF(artigo.getAssunto());
                dos.writeUTF(artigo.getTexto());
            }
            dos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                dos.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
